package com.vectz.autogen.action;

import java.io.IOException;
import java.util.ArrayList;

import com.vectz.autogen.model.Entity;
import com.vectz.autogen.model.EntityOverview;
import com.vectz.autogen.util.Constant;
import com.vectz.autogen.util.Log;
import com.vectz.autogen.util.Util;

/**
 * @author ai-t.vn
 * 
 */
public class JsfConfirmCodegenAction {
	ArrayList<Entity> listField=null;
	EntityOverview table = null;
	String outputFolder = "";
	public JsfConfirmCodegenAction(ArrayList<Entity> listField, EntityOverview table, String outputFolder) {
		this.listField = listField;
		this.table = table;
		this.outputFolder = outputFolder;
		genernateJsfFile();
	}

	/*
	 * Generate jsf file
	 */
	public void genernateJsfFile() {
		try {
			String tmplFormContent = Util.readTextFileContent(Util
					.getTmplPath("jsf", "Form-ConfirmEntity.txt"));
			String tmplFieldContent = Util.readTextFileContent(Util
					.getTmplPath("jsf", "ConfirmEntity.txt"));

			// add content path
			String listFieldForm = genernateFieldForm(listField,
					tmplFieldContent);
			tmplFormContent = tmplFormContent.replaceAll("xxxxFormId",
					table.getScreenId());
			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxBodyContent", listFieldForm);
			tmplFormContent = tmplFormContent.replaceAll("xxxxPersist",
					Util.buildButtonAction(table.getName(), "persist"));
			tmplFormContent = tmplFormContent.replaceAll("xxxxBack", Util
					.buildButtonAction(table.getName(), "back"));
			
			String fileLocation = new StringBuffer().append(outputFolder)
			.append(System.getProperty("file.separator"))
			.append("WebContent")
			.append(System.getProperty("file.separator"))
			.append(Util.getWebbaseDir(table.getDestScreenPath()))
			.append(System.getProperty("file.separator"))
			.append("confirm")
			.append(Util.lowerCaseFirstChar(table.getName()))
			.append(".xhtml").toString();
			
			Util.writeJavaFile(fileLocation,tmplFormContent);
			Log.log("Finish generate jsf file to:"+ fileLocation);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/*
	 * Generate list field name
	 */
	public String genernateFieldForm(ArrayList<Entity> listField,
			String tmplContent) {
		StringBuffer content = new StringBuffer();

		if (listField == null && listField.size() == 0)
			return "";
		Entity field = null;
		for (int i = 0; i < listField.size(); i++) {
			field = (Entity) listField.get(i);
			//check only display=show then add new component
			if (field.getDisplay().equalsIgnoreCase("show")) {
				String tmp = tmplContent;
				tmp = tmp.replaceAll("xxxxLableName", field.getLogicalName());
				tmp = tmp.replaceAll("xxxxInputValue", getInputValue(field));
				
				content.append(tmp);
			}
		}

		return content.toString();
	}

	/**
	 * Get input value with pattern: #{entityAction.entity.field}
	 * 
	 * @param entity
	 * @return input value
	 */
	public String getInputValue(Entity entity) {
		return new StringBuffer().append("#{").append(
				Util.lowerCaseFirstChar(table.getName())).append("Action.")
				.append(Util.lowerCaseFirstChar(table.getName())).append(".")
				.append(entity.getName()).append("}").toString();
	}


}
